package com.mzy.linear_struct.hot100.链表;

import com.sun.scenario.effect.Merge;

/**
 * @Author: mizy3
 * @Date: 2024/07/31/下午4:57
 * @Description:
 */
public class Pro21合并两个有序链表 {

    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode answer = new ListNode();
        ListNode head = answer;

        while (list1 != null && list2 != null) {
            if (list1.val < list2.val) {
                answer.next = list1;
                list1 = list1.next;
            }else {
                answer.next = list2;
                list2 = list2.next;
            }
            answer = answer.next;
        }

        while (list1 != null) {
            answer.next = list1;
            list1 = list1.next;
            answer = answer.next;
        }

        while (list2 != null) {
            answer.next = list2;
            list2 = list2.next;
            answer = answer.next;
        }
        return head.next;
    }

    public static void main(String[] args) {
        ListNode head1 = new ListNode(-9, new ListNode(3));
        ListNode head2 = new ListNode(5, new ListNode(7));
        ListNode listNode = new Pro21合并两个有序链表().mergeTwoLists(head1, head2);
        System.out.println(listNode);
    }
}
